package cn.tom.controller;

import cn.tom.tools.TokenAndUser;
import cn.tom.tools.XResp;
import cn.tom.entity.XUser;
import cn.tom.tools.XJson;
import cn.tom.service.LargeFileService;
import cn.tom.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api/user")
public class UserController {
        private Logger log = LoggerFactory.getLogger(this.getClass());
        @Autowired
        private UserService userService;
        @Autowired
        private LargeFileService largeFileService;
        @Autowired
        private PasswordEncoder passwordEncoder;


        @GetMapping("/ShipInfo")
        public XJson getUserShipInfo(){
            XUser user = ( XUser) TokenAndUser.findOne().getData();
            Map<String, Object> map = new HashMap<>();
            map.put("name"  , user.getNickname());
            map.put("phone" , user.getPhone());
            map.put("addr"  , user.getAddr());
            return XResp.pack(200,"用户发货信息",map);
        }
        @GetMapping("/info")
        public XJson getUserInfo() {
            XUser user = ( XUser) TokenAndUser.findOne().getData();
            System.out.println(2);
            return XResp.pack(200,"用户信息", user);
        }

        @PostMapping("/updatePwd")
        public XJson updatePwd(String oldPwd, String newPwd) {
            log.info("更新密码：" + oldPwd);
            XUser myUser = (XUser) TokenAndUser.findOne().getData();
            boolean b =  passwordEncoder.matches(oldPwd,myUser.getPassword());
            if(!b) return XResp.pack(501,"请输入正确的旧密码","");
            myUser.setPassword(newPwd);
            int x = userService.update(myUser);
            if ( x == 0 ) return XResp.pack(501,"密码修改失败","");
            return XResp.pack(200,"密码成功修改","");
        }

        @PostMapping("/updateImg")
        public XJson updatePic(@RequestParam(value="myimg", required = false) MultipartFile file) throws IOException{
            if (file ==  null || file.getSize()<0)
                return XResp.pack(501,"图片上传有误， 更新失败","");
            XUser myUser = (XUser) TokenAndUser.findOne().getData();
            String uuid = myUser.getImg();
            largeFileService.delete(uuid);  //先删除
            largeFileService.add(uuid,file.getOriginalFilename(),file.getBytes());
            XUser user = new XUser();
            user.setId(myUser.getId());
            user.setImg(uuid);
            userService.update(user);
            uuid = uuid + "?a=" + System.currentTimeMillis()%10000000;
            return XResp.pack(200,"更新图像成功", uuid);   //uuid 新的图片 id
        }

        @PostMapping("/updateShip")
        public XJson updateShip(XUser xUser){
            XUser myUser = (XUser) TokenAndUser.findOne().getData();
            XUser user = new XUser();
            user.setId(        myUser.getId());
            user.setNickname(  xUser.getNickname());
            user.setPhone(     xUser.getPhone());
            user.setAddr(      xUser.getAddr());
            int x = userService.update(user);
            if( x == 0 )
                return XResp.pack(501,"信息修改失败","");
            return XResp.pack(200,"信息成功修改","");
        }
    }


